package com.wyix.controller;

import com.wyix.service.UserService;
import com.wyix.domian.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author wangdx
 * @since 2025/8/21
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    //新增用户
    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.save(user);
    }

    //查询所有用户
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    //根据ID查询用户
    @GetMapping("/{id}")
    public Optional<User> findById(@PathVariable Long id) {
        return userService.findById(id);
    }

    //根据年龄查询用户
    @GetMapping("/age/{age}")
    public List<User> findByAge(@PathVariable Integer age) {
        return userService.findByAge(age);
    }

    //更新用户
    @PutMapping
    public User updateUser(@RequestBody User user) {
        return userService.update(user);
    }

    //删除用户
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.deleteById(id);
    }
}
